﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JcomTesters.Releases
{
    [jDesc("所有命令",ok:true,
        desc:jConsts.desc_help,
        Examples = jConsts.eg_of_help)]
    class HelpCmd : Ab1Tester
    {
        public override void Run(string[] args)
        {
            var name = "";
            foreach (var item in args)
            {
                if (item.StartsWith("-debug")) { this.isDebug = true; continue; }
                else if (item.StartsWith("-")) { continue; }
                else { name = item; }
            }
            var tys = jtool.GetTesterTypes(name).OrderBy(t => t.Name);
            if (tys.Count() == 1)
            {
                //显示单个命令详情
                var n1Color = ConsoleColor.Yellow;//新颜色
                ConsoleColor currentForeColor = Console.ForegroundColor;//备份原颜色

                Console.ForegroundColor = n1Color;//换颜色打印
                                                  //--------------------0293a

                //显示详细
                foreach (var item in tys)
                {
                    var attr = item.GetCustomAttributes(typeof(jDescAttribute), false).FirstOrDefault() as jDescAttribute;
                    Console.WriteLine(
                        " 命令: " + item.Name.Substring(0, item.Name.Length - 6)
                        );
                    Console.WriteLine(
                        " Name: " + (attr == null ? "" : attr.Name) + " " + (attr != null && attr.IsDelete ? "[已弃用]" : ""));
                    Console.WriteLine(
                        " Desc: " + (attr == null ? "" : attr.Desc));
                    Console.WriteLine(
                        " Examples: " + (attr == null ? "" : attr.Examples));
                    Console.WriteLine(
                        " 已完成: " + (attr == null ? false : attr.IsOk));
                    //Console.WriteLine(
                    //    " 已弃用: " + (attr != null && attr.IsDelete?"            是":"否"));
                    //
                    Console.Write(
                        " 参数: ");
                    for (int i = 0; attr.Args != null && i < attr.Args.Length; i++)
                    {
                        if (i % 2 == 0) { Console.WriteLine(); Console.WriteLine("   name: " + attr.Args[i]); }
                        else { Console.WriteLine("   desc: " + attr.Args[i]); }
                    }
                    if (attr.Args == null || attr.Args.Length == 0) Console.WriteLine(" (无)");
                }
                Console.WriteLine("更多详细参见: help [cmdname]");
                //Console.WriteLine("更多详细参见: cmds [cmdname]");

                //--------------------0293b
                Console.ForegroundColor = currentForeColor;//还原原颜色

                return;
            }
            else
            {
                //显示所有命令
                var n1Color = ConsoleColor.Yellow;//新颜色
                ConsoleColor currentForeColor = Console.ForegroundColor;//备份原颜色

                Console.ForegroundColor = n1Color;//换颜色打印
                                                  //--------------------0293a

                //Console.WriteLine(printText);
                Console.WriteLine(
                    " #以下命令不分大小写:\r\n" +
                        string.Format(" {0,-30}   {1,-50}",
                        "命令", "说明1") + "\r\n" +
                        string.Format(" {0,-30}   {1,-50}",
                        "--------", "--------"));
                foreach (var item in tys)
                {
                    var attr = item.GetCustomAttributes(typeof(jDescAttribute), false).FirstOrDefault() as jDescAttribute;
                    var p2= item.Name.EndsWith("Tester")?"Tester": item.Name.EndsWith("Cmd")?"Cmd":"";
                    jThrow.whenEmpty(p2, "错误识别303");
                    Console.WriteLine(
                        string.Format(" {0,-30}   {1,-50}",
                        item.Name.Substring(0, item.Name.Length - p2.Length)
                        , (attr == null ? "" : attr.Name)));
                }
                Console.WriteLine("更多详细参见: help [cmdname]");
                //Console.WriteLine("更多详细参见: cmds [cmdname]");

                //--------------------0293b
                Console.ForegroundColor = currentForeColor;//还原原颜色
            }
        }
    }
}
